package com.adingxiong.cft.websocket;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName HeartBeatHandler
 * @Description TODO
 * @Author xiongchao
 * @Date 2021/4/8 13:40
 **/
@Slf4j
public class HeartBeatHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

        //心跳机制监听
        if(evt instanceof IdleStateEvent) {
            IdleStateEvent event = (IdleStateEvent) evt;

            if(event.state() == IdleState.READER_IDLE) {
                log.info("数据读取空闲中");
            } else if ( event.state() == IdleState.WRITER_IDLE){
                log.info("写数据空闲中");
            } else if (event.state() == IdleState.ALL_IDLE) {
                Channel channel = ctx.channel();
                channel.close();

                log.info("监听当前连接数:{}",MyServerHandler.clients.size());
            }
        }
    }
}
